﻿namespace BEGDC.CC.Web.UserControls.CR
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Collections;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using BEGDC.CC.BL.CCFlow.CR;
    using BEGDC.CC.Entity;
    using BEGDC.CC.BL.Workflow;
    using BEGDC.CC.BL.Common;
    using BEGDC.Utility.Castle;
    using BEGDC.CC.BL.Common.Query;

    /// <summary>
    ///		Summary description for CloseApp.
    /// </summary>
    public class CloseApp : BaseCR
    {
        #region Variables
        protected System.Web.UI.WebControls.CheckBoxList CBL_ASBLeader;

        protected System.Web.UI.WebControls.Button Btn_Submit;
        //protected System.Web.UI.WebControls.Button Btn_Save;
        protected System.Web.UI.WebControls.Button Btn_Query;
        protected BEGDC.CC.Web.UserControls.CR.NormalCtrl NormalCtrl;
        protected BEGDC.CC.Web.UserControls.CR.PrintCtrl PrintCtrl;
        protected System.Web.UI.WebControls.TextBox Ctl_EZVisitID;
        protected System.Web.UI.WebControls.TextBox Ctl_LegalID;
        protected System.Web.UI.WebControls.TextBox Ctl_Comment;
        protected BEGDC.CC.Web.UserControls.CR.EZCtrl EZCtrl;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_EZVISID;
        protected System.Web.UI.HtmlControls.HtmlTableRow TR_LegalID;
        protected System.Web.UI.WebControls.TextBox tbOperationFlag;
        protected System.Web.UI.WebControls.TextBox tbOperationContent;
        protected System.Web.UI.WebControls.TextBox tbComment;
        protected BEGDC.CC.Web.Common.StampCtrl StampCtrl1;
        protected System.Web.UI.WebControls.Label lbAppNo;
        #endregion

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Session[SessionFlag.CustomerSession] = null;
                Session[SessionFlag.AccompanySession] = null;
                Session[SessionFlag.HotelSession] = null;
                Session[SessionFlag.CompanyCarSession] = null;
                Session[SessionFlag.LeaseCarSession] = null;
                Session[SessionFlag.AccompanySessionEZ] = null;
                Session[SessionFlag.CustomerSessionEZ] = null;
                Session[SessionFlag.CompanyCarSessionEZ] = null;
                Session[SessionFlag.LeaseCarSessionEZ] = null;
                Session[SessionFlag.HotelSessionEZ] = null;
                Session[SessionFlag.ContractInfo] = null;
                Session[SessionFlag.CostCenter] = null;

                base.PreInitForm();

                //this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
                this.Btn_Submit.Attributes["onclick"] = "javascript:OnSubmit();";
                this.Btn_Query.Attributes.Add("onclick", "javascript:OpenQueryPane(" + Request.QueryString["AppID"] + ");");
                ButtonStatusSetting();

                InitForm();

                this.BindASBLeaders();
            }
        }



        #region SetStamp
        //		private void SetStamp()
        //		{
        //			IQuery queryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
        //			DataTable table = queryService.QueryActionLog( this.AppID );
        //			StampCtrl1.SetDataSource( table );
        //		}
        #endregion

        #region ControlStatusSetting
        private void ControlStatusSetting(Accessibility access)
        {
            if (access == Accessibility.Readonly)
            {
                this.DisableChildrenControls(this);
                //this.stampCtrl1.Visible = false;
            }
        }
        #endregion

        #region InitForm
        private void InitForm()
        {
            bool isReadOnly = true;
            long flowId = this.LoadApplication();

            if (this.Application.AppNo != string.Empty)
                this.lbAppNo.Text = " Application No:" + this.Application.AppNo;

            if (this.Application != null)
            {
                if (!this.Application.IsTransition)
                {
                    EZCtrl.Visible = false;
                    //PrintCtrl.Visible = false;
                    NormalCtrl.Visible = true;

                    NormalCtrl.isReadOnly = isReadOnly;
                    NormalCtrl.App = this.Application;

                    TR_EZVISID.Visible = false;
                    TR_LegalID.Visible = false;
                }
                else
                {
                    EZCtrl.Visible = true;
                    NormalCtrl.Visible = false;
                    EZCtrl.isReadOnly = isReadOnly;

                    this.Ctl_EZVisitID.Text = this.Application.EZVisitReqNo;
                    this.Ctl_LegalID.Text = this.Application.LGDNo;
                    //this.Btn_Export.Visible = true;
                }
                this.Ctl_Comment.Text = this.Application.Summary;
            }
        }
        #endregion

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Query.Click += new EventHandler(Btn_Query_Click);
            //this.Btn_Save.Click += new EventHandler(Btn_Save_Click);
            this.Btn_Submit.Click += new EventHandler(Btn_Submit_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        //#region Save
        //void Btn_Save_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        //this.SaveASBLeaders();

        //        ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
        //        BEGDC.CC.Entity.Application App = CRService.GetApplication(this.AppID);
        //        if (App.IsTransition)
        //        {
        //            App.EZVisitReqNo = this.Ctl_EZVisitID.Text;
        //            App.LGDNo = this.Ctl_LegalID.Text;
        //            App.Summary = this.Ctl_Comment.Text;

        //            CRService.SaveEZCloseApp(App);

        //            Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
        //        }
        //        else
        //        {
        //            App.Summary = this.Ctl_Comment.Text;

        //            CRService.SaveNormalCloseApp(App);

        //            Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
        //        }
        //    }
        //    catch (Exception exp)
        //    {
        //        //throw exp;
        //        Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
        //    }
        //}
        //#endregion

        #region Submit
        void Btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                BEGDC.CC.Entity.Application App = CRService.GetApplication(this.AppID);

                this.SaveASBLeaders();

                if (App.IsTransition)
                {
                    if (this.Ctl_EZVisitID.Text != "" && this.Ctl_LegalID.Text != "")
                    {
                        App.EZVisitReqNo = this.Ctl_EZVisitID.Text;
                        App.LGDNo = this.Ctl_LegalID.Text;
                        App.Summary = this.Ctl_Comment.Text;

                        CRService.SubmitEZCloseApp(App, true, this.tbComment.Text);

                        Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
                    }
                    else
                    {
                        Page.RegisterClientScriptBlock("", "<script>alert('Please fill in EZVisit ID, Legal ID and Knowledge Sharing');</script>");
                        return;
                    }
                }
                else
                {
                    //					if(this.Ctl_Comment.Text!="")
                    //					{
                    App.Summary = this.Ctl_Comment.Text;
                    CRService.SubmitNormalCloseApp(App, this.tbComment.Text);
                    Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
                    //					}
                    //					else
                    //					{
                    //						Page.RegisterClientScriptBlock("","<script>alert('Please fill in Knowledge Sharing!');</script>");
                    //						return;
                    //					}
                }
            }
            catch (Exception exp)
            {
                //throw exp;
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
            }
        }
        #endregion

        #region Export
        private void Btn_Export_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            try
            {
                ExcelTool.ICCExcelExport excelExport;
                if (this.Application.IsTransition)
                    excelExport = new ExcelTool.ExportCRFormA();
                else
                    excelExport = new ExcelTool.ExportCRFormB();
                excelExport.Export(this.Page, this.AppID);
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");

            }
            catch (BEGDC.Utility.ExceptionManagement.GDCException exp)
            {
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
            }
        }
        #endregion

        #region Query
        void Btn_Query_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.tbOperationFlag.Text == "query")
                {
                    ICR CRService = AppContainer.Instance[ServiceConst.CRService] as ICR;
                    CRService.Query(Convert.ToInt64(Request.QueryString["FlowID"]), this.tbOperationContent.Text, Session["Comment"].ToString());
                }
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');window.close();</script>");
            }
            catch
            {
                Page.RegisterClientScriptBlock("", "<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");
            }
        }
        #endregion

        #region ButtonStatusSetting
        protected override void ButtonStatusSetting()
        {
            if (this.Access == null)
                return;

            //this.Btn_Save.Visible = this.Access.IsButtonVisible(CRTransitionList.Save);
            this.Btn_Submit.Visible = this.Access.IsButtonVisible(CRTransitionList.Close);
            //this.Btn_Query.Visible = this.Access.IsButtonVisible(CRTransitionList.Query);
            this.Btn_Query.Visible = false;
        }
        #endregion

        private void BindASBLeaders()
        {
            ////CBL_ASBLeader
            //BEGDC.CC.BL.Common.IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as BEGDC.CC.BL.Common.IUtility;
            //IList list = utility.BuildCheckBoxASBLeaders();

            //CBL_ASBLeader.DataSource = list;
            //CBL_ASBLeader.DataValueField = "ID";
            //CBL_ASBLeader.DataTextField = "NameCN";
            //CBL_ASBLeader.DataBind();

            BEGDC.CC.BL.Common.Query.IQuery query = AppContainer.Instance[ServiceConst.QueryService] as BEGDC.CC.BL.Common.Query.IQuery;
            DataTable table = query.GetDicItemByTypeName(BEGDC.CC.BL.Common.DicType.ASBLeaders);
            this.CBL_ASBLeader.DataSource = table.DefaultView;
            this.CBL_ASBLeader.DataValueField = "DicItemID";
            this.CBL_ASBLeader.DataTextField = "NameChinese";
            this.CBL_ASBLeader.DataBind();
        }

        private void SaveASBLeaders()
        {
            int Count = this.CBL_ASBLeader.Items.Count;
            System.Web.UI.WebControls.ListItem item = null;
            for (int i = 0; i < Count; i++)
            {
                item = this.CBL_ASBLeader.Items[i] as ListItem;
                if (item.Selected)
                {
                    BEGDC.CC.BL.Common.IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as BEGDC.CC.BL.Common.IUtility;
                    utility.SaveASBLeaders(this.Application.Id, int.Parse(item.Value));
                }
            }
        }
    }
}
